import { Shelf, ShelfDetail, WarehouseItem, WarehouseMap } from '../types/warehouse';
import authService from './api/auth'; // 直接导入已实例化的authService



// 库位编号与真实ID的映射关系
const locationIdMapping: Record<string, string> = {
  // A区 (公物仓一)
  'AQ_HJ_001_1_1': '737354428520349697',
  'AQ_HJ_001_1_2': '737354428520349698',
  'AQ_HJ_001_1_3': '737354428520349699',
  'AQ_HJ_001_2_1': '737354428520349700',
  'AQ_HJ_001_2_2': '737354428520349701',
  'AQ_HJ_001_2_3': '737354428520349702',
  'AQ_HJ_001_3_1': '737354428520349703',
  'AQ_HJ_001_3_2': '737354428520349704',
  'AQ_HJ_001_3_3': '737354428520349705',
  'AQ_HJ_001_4_1': '737354428520349706',
  'AQ_HJ_001_4_2': '737354428520349707',
  'AQ_HJ_001_4_3': '737354428520349708',
  'AQ_HJ_002_1_1': '737354337336180737',
  'AQ_HJ_002_1_2': '737354337336180738',
  'AQ_HJ_002_1_3': '737354337336180739',
  'AQ_HJ_002_2_1': '737354337336180740',
  'AQ_HJ_002_2_2': '737354337336180741',
  'AQ_HJ_002_2_3': '737354337336180742',
  'AQ_HJ_002_3_1': '737354337336180743',
  'AQ_HJ_002_3_2': '737354337336180744',
  'AQ_HJ_002_3_3': '737354337336180745',
  'AQ_HJ_002_4_1': '737354337336180746',
  'AQ_HJ_002_4_2': '737354337336180747',
  'AQ_HJ_002_4_3': '737354337336180748',
  'AQ_HJ_003_1_1': '737354157694140417',
  'AQ_HJ_003_1_2': '737354157694140418',
  'AQ_HJ_003_1_3': '737354157694140419',
  'AQ_HJ_003_2_1': '737354157694140420',
  'AQ_HJ_003_2_2': '737354157694140421',
  'AQ_HJ_003_2_3': '737354157694140422',
  'AQ_HJ_003_3_1': '737354157694140423',
  'AQ_HJ_003_3_2': '737354157694140424',
  'AQ_HJ_003_3_3': '737354157694140425',
  'AQ_HJ_003_4_1': '737354157694140426',
  'AQ_HJ_003_4_2': '737354157694140427',
  'AQ_HJ_003_4_3': '737354157694140428',
  'AQ_HJ_004_1_1': '737353803363532801',
  'AQ_HJ_004_1_2': '737353803363532802',
  'AQ_HJ_004_1_3': '737353803363532803',
  'AQ_HJ_004_2_1': '737353803363532804',
  'AQ_HJ_004_2_2': '737353803363532805',
  'AQ_HJ_004_2_3': '737353803363532806',
  'AQ_HJ_004_3_1': '737353803363532807',
  'AQ_HJ_004_3_2': '737353803363532808',
  'AQ_HJ_004_3_3': '737353803363532809',
  'AQ_HJ_004_4_1': '737353803363532810',
  'AQ_HJ_004_4_2': '737353803363532811',
  'AQ_HJ_004_4_3': '737353803363532812',
  'AQ_HJ_005_1_1': '737352985608470529',
  'AQ_HJ_005_1_2': '737352985608470530',
  'AQ_HJ_005_1_3': '737352985608470531',
  'AQ_HJ_005_2_1': '737352985608470532',
  'AQ_HJ_005_2_2': '737352985608470533',
  'AQ_HJ_005_2_3': '737352985608470534',
  'AQ_HJ_005_3_1': '737352985608470535',
  'AQ_HJ_005_3_2': '737352985608470536',
  'AQ_HJ_005_3_3': '737352985608470537',
  'AQ_HJ_005_4_1': '737352985608470538',
  'AQ_HJ_005_4_2': '737352985608470539',
  'AQ_HJ_005_4_3': '737352985608470540',
  'AQ_HJ_006_1_1': '737352905098805249',
  'AQ_HJ_006_1_2': '737352905098805250',
  'AQ_HJ_006_1_3': '737352905098805251',
  'AQ_HJ_006_2_1': '737352905098805252',
  'AQ_HJ_006_2_2': '737352905098805253',
  'AQ_HJ_006_2_3': '737352905098805254',
  'AQ_HJ_006_3_1': '737352905098805255',
  'AQ_HJ_006_3_2': '737352905098805256',
  'AQ_HJ_006_3_3': '737352905098805257',
  'AQ_HJ_006_4_1': '737352905098805258',
  'AQ_HJ_006_4_2': '737352905098805259',
  'AQ_HJ_006_4_3': '737352905098805260',
  'AQ_HJ_007_1_1': '737352294584303617',
  'AQ_HJ_007_1_2': '737352294584303618',
  'AQ_HJ_007_1_3': '737352294584303619',
  'AQ_HJ_007_2_1': '737352294584303620',
  'AQ_HJ_007_2_2': '737352294584303621',
  'AQ_HJ_007_2_3': '737352294584303622',
  'AQ_HJ_007_3_1': '737352294584303623',
  'AQ_HJ_007_3_2': '737352294584303624',
  'AQ_HJ_007_3_3': '737352294584303625',
  'AQ_HJ_007_4_1': '737352294584303626',
  'AQ_HJ_007_4_2': '737352294584303627',
  'AQ_HJ_007_4_3': '737352294584303628',
  'AQ_HJ_008_1_1': '737352202045374465',
  'AQ_HJ_008_1_2': '737352202045374466',
  'AQ_HJ_008_1_3': '737352202045374467',
  'AQ_HJ_008_2_1': '737352202045374468',
  'AQ_HJ_008_2_2': '737352202045374469',
  'AQ_HJ_008_2_3': '737352202045374470',
  'AQ_HJ_008_3_1': '737352202045374471',
  'AQ_HJ_008_3_2': '737352202045374472',
  'AQ_HJ_008_3_3': '737352202045374473',
  'AQ_HJ_008_4_1': '737352202045374474',
  'AQ_HJ_008_4_2': '737352202045374475',
  'AQ_HJ_008_4_3': '737352202045374476',
  'AQ_HJ_009_1_1': '737352110022344705',
  'AQ_HJ_009_1_2': '737352110022344706',
  'AQ_HJ_009_1_3': '737352110022344707',
  'AQ_HJ_009_2_1': '737352110022344708',
  'AQ_HJ_009_2_2': '737352110022344709',
  'AQ_HJ_009_2_3': '737352110022344710',
  'AQ_HJ_009_3_1': '737352110022344711',
  'AQ_HJ_009_3_2': '737352110022344712',
  'AQ_HJ_009_3_3': '737352110022344713',
  'AQ_HJ_009_4_1': '737352110022344714',
  'AQ_HJ_009_4_2': '737352110022344715',
  'AQ_HJ_009_4_3': '737352110026539008',
  'AQ_HJ_010_1_1': '737352027377778689',
  'AQ_HJ_010_1_2': '737352027377778690',
  'AQ_HJ_010_1_3': '737352027377778691',
  'AQ_HJ_010_2_1': '737352027377778692',
  'AQ_HJ_010_2_2': '737352027377778693',
  'AQ_HJ_010_2_3': '737352027377778694',
  'AQ_HJ_010_3_1': '737352027377778695',
  'AQ_HJ_010_3_2': '737352027377778696',
  'AQ_HJ_010_3_3': '737352027377778697',
  'AQ_HJ_010_4_1': '737352027377778698',
  'AQ_HJ_010_4_2': '737352027377778699',
  'AQ_HJ_010_4_3': '737352027377778700',
  'AQ_HJ_011_1_1': '737351902362353665',
  'AQ_HJ_011_1_2': '737351902362353666',
  'AQ_HJ_011_1_3': '737351902362353667',
  'AQ_HJ_011_2_1': '737351902362353668',
  'AQ_HJ_011_2_2': '737351902362353669',
  'AQ_HJ_011_2_3': '737351902362353670',
  'AQ_HJ_011_3_1': '737351902362353671',
  'AQ_HJ_011_3_2': '737351902362353672',
  'AQ_HJ_011_3_3': '737351902362353673',
  'AQ_HJ_011_4_1': '737351902362353674',
  'AQ_HJ_011_4_2': '737351902362353675',
  'AQ_HJ_011_4_3': '737351902362353676',
  'AQ_HJ_012_1_1': '737351704198266881',
  'AQ_HJ_012_1_2': '737351704198266882',
  'AQ_HJ_012_1_3': '737351704198266883',
  'AQ_HJ_012_2_1': '737351704198266884',
  'AQ_HJ_012_2_2': '737351704198266885',
  'AQ_HJ_012_2_3': '737351704198266886',
  'AQ_HJ_012_3_1': '737351704198266887',
  'AQ_HJ_012_3_2': '737351704198266888',
  'AQ_HJ_012_3_3': '737351704198266889',
  'AQ_HJ_012_4_1': '737351704198266890',
  'AQ_HJ_012_4_2': '737351704198266891',
  'AQ_HJ_012_4_3': '737351704198266892',
  'AQ_HJ_013_1_1': '737351601261658113',
  'AQ_HJ_013_1_2': '737351601261658114',
  'AQ_HJ_013_1_3': '737351601261658115',
  'AQ_HJ_013_2_1': '737351601261658116',
  'AQ_HJ_013_2_2': '737351601261658117',
  'AQ_HJ_013_2_3': '737351601261658118',
  'AQ_HJ_013_3_1': '737351601261658119',
  'AQ_HJ_013_3_2': '737351601261658120',
  'AQ_HJ_013_3_3': '737351601261658121',
  'AQ_HJ_013_4_1': '737351601261658122',
  'AQ_HJ_013_4_2': '737351601261658123',
  'AQ_HJ_013_4_3': '737351601261658124',
  'BQ_HJ_001_1_1': '737356636519415809',
  'BQ_HJ_001_1_2': '737356636519415810',
  'BQ_HJ_001_1_3': '737356636519415811',
  'BQ_HJ_001_2_1': '737356636519415812',
  'BQ_HJ_001_2_2': '737356636519415813',
  'BQ_HJ_001_2_3': '737356636519415814',
  'BQ_HJ_001_3_1': '737356636519415815',
  'BQ_HJ_001_3_2': '737356636519415816',
  'BQ_HJ_001_3_3': '737356636519415817',
  'BQ_HJ_001_4_1': '737356636519415818',
  'BQ_HJ_001_4_2': '737356636519415819',
  'BQ_HJ_001_4_3': '737356636519415820',
  'BQ_HJ_002_1_1': '737356542034329601',
  'BQ_HJ_002_1_2': '737356542034329602',
  'BQ_HJ_002_1_3': '737356542034329603',
  'BQ_HJ_002_2_1': '737356542034329604',
  'BQ_HJ_002_2_2': '737356542034329605',
  'BQ_HJ_002_2_3': '737356542034329606',
  'BQ_HJ_002_3_1': '737356542034329607',
  'BQ_HJ_002_3_2': '737356542034329608',
  'BQ_HJ_002_3_3': '737356542034329609',
  'BQ_HJ_002_4_1': '737356542034329610',
  'BQ_HJ_002_4_2': '737356542034329611',
  'BQ_HJ_002_4_3': '737356542034329612',
  'BQ_HJ_003_1_1': '737356455795245057',
  'BQ_HJ_003_1_2': '737356455795245058',
  'BQ_HJ_003_1_3': '737356455795245059',
  'BQ_HJ_003_2_1': '737356455795245060',
  'BQ_HJ_003_2_2': '737356455795245061',
  'BQ_HJ_003_2_3': '737356455795245062',
  'BQ_HJ_003_3_1': '737356455795245063',
  'BQ_HJ_003_3_2': '737356455795245064',
  'BQ_HJ_003_3_3': '737356455795245065',
  'BQ_HJ_003_4_1': '737356455795245066',
  'BQ_HJ_003_4_2': '737356455795245067',
  'BQ_HJ_003_4_3': '737356455795245068',
  'BQ_HJ_004_1_1': '737356336949641217',
  'BQ_HJ_004_1_2': '737356336949641218',
  'BQ_HJ_004_1_3': '737356336949641219',
  'BQ_HJ_004_2_1': '737356336949641220',
  'BQ_HJ_004_2_2': '737356336949641221',
  'BQ_HJ_004_2_3': '737356336949641222',
  'BQ_HJ_004_3_1': '737356336949641223',
  'BQ_HJ_004_3_2': '737356336949641224',
  'BQ_HJ_004_3_3': '737356336949641225',
  'BQ_HJ_004_4_1': '737356336949641226',
  'BQ_HJ_004_4_2': '737356336949641227',
  'BQ_HJ_004_4_3': '737356336949641228',
  'BQ_HJ_005_1_1': '737356225712504833',
  'BQ_HJ_005_1_2': '737356225712504834',
  'BQ_HJ_005_1_3': '737356225712504835',
  'BQ_HJ_005_2_1': '737356225712504836',
  'BQ_HJ_005_2_2': '737356225712504837',
  'BQ_HJ_005_2_3': '737356225712504838',
  'BQ_HJ_005_3_1': '737356225712504839',
  'BQ_HJ_005_3_2': '737356225712504840',
  'BQ_HJ_005_3_3': '737356225712504841',
  'BQ_HJ_005_4_1': '737356225712504842',
  'BQ_HJ_005_4_2': '737356225712504843',
  'BQ_HJ_005_4_3': '737356225712504844',
  'BQ_HJ_006_1_1': '737356118728392705',
  'BQ_HJ_006_1_2': '737356118728392706',
  'BQ_HJ_006_1_3': '737356118728392707',
  'BQ_HJ_006_2_1': '737356118728392708',
  'BQ_HJ_006_2_2': '737356118728392709',
  'BQ_HJ_006_2_3': '737356118728392710',
  'BQ_HJ_006_3_1': '737356118728392711',
  'BQ_HJ_006_3_2': '737356118728392712',
  'BQ_HJ_006_3_3': '737356118728392713',
  'BQ_HJ_006_4_1': '737356118728392714',
  'BQ_HJ_006_4_2': '737356118728392715',
  'BQ_HJ_006_4_3': '737356118728392716',
  'BQ_HJ_007_1_1': '737356016999743489',
  'BQ_HJ_007_1_2': '737356016999743490',
  'BQ_HJ_007_1_3': '737356016999743491',
  'BQ_HJ_007_2_1': '737356016999743492',
  'BQ_HJ_007_2_2': '737356016999743493',
  'BQ_HJ_007_2_3': '737356016999743494',
  'BQ_HJ_007_3_1': '737356016999743495',
  'BQ_HJ_007_3_2': '737356016999743496',
  'BQ_HJ_007_3_3': '737356016999743497',
  'BQ_HJ_007_4_1': '737356016999743498',
  'BQ_HJ_007_4_2': '737356016999743499',
  'BQ_HJ_007_4_3': '737356016999743500',
  'BQ_HJ_008_1_1': '737355899047526401',
  'BQ_HJ_008_1_2': '737355899047526402',
  'BQ_HJ_008_1_3': '737355899047526403',
  'BQ_HJ_008_2_1': '737355899047526404',
  'BQ_HJ_008_2_2': '737355899047526405',
  'BQ_HJ_008_2_3': '737355899047526406',
  'BQ_HJ_008_3_1': '737355899047526407',
  'BQ_HJ_008_3_2': '737355899047526408',
  'BQ_HJ_008_3_3': '737355899047526409',
  'BQ_HJ_008_4_1': '737355899047526410',
  'BQ_HJ_008_4_2': '737355899047526411',
  'BQ_HJ_008_4_3': '737355899047526412',
  'BQ_HJ_009_1_1': '737355794546442241',
  'BQ_HJ_009_1_2': '737355794546442242',
  'BQ_HJ_009_1_3': '737355794546442243',
  'BQ_HJ_009_2_1': '737355794546442244',
  'BQ_HJ_009_2_2': '737355794546442245',
  'BQ_HJ_009_2_3': '737355794546442246',
  'BQ_HJ_009_3_1': '737355794546442247',
  'BQ_HJ_009_3_2': '737355794546442248',
  'BQ_HJ_009_3_3': '737355794546442249',
  'BQ_HJ_009_4_1': '737355794546442250',
  'BQ_HJ_009_4_2': '737355794546442251',
  'BQ_HJ_009_4_3': '737355794546442252',
  'BQ_HJ_010_1_1': '737355260900950017',
  'BQ_HJ_010_1_2': '737355260900950018',
  'BQ_HJ_010_1_3': '737355260900950019',
  'BQ_HJ_010_2_1': '737355260900950020',
  'BQ_HJ_010_2_2': '737355260900950021',
  'BQ_HJ_010_2_3': '737355260900950022',
  'BQ_HJ_010_3_1': '737355260900950023',
  'BQ_HJ_010_3_2': '737355260900950024',
  'BQ_HJ_010_3_3': '737355260900950025',
  'BQ_HJ_010_4_1': '737355260900950026',
  'BQ_HJ_010_4_2': '737355260900950027',
  'BQ_HJ_010_4_3': '737355260900950028',
  'BQ_HJ_011_1_1': '737355134077779969',
  'BQ_HJ_011_1_2': '737355134077779970',
  'BQ_HJ_011_1_3': '737355134077779971',
  'BQ_HJ_011_2_1': '737355134077779972',
  'BQ_HJ_011_2_2': '737355134077779973',
  'BQ_HJ_011_2_3': '737355134077779974',
  'BQ_HJ_011_3_1': '737355134077779975',
  'BQ_HJ_011_3_2': '737355134077779976',
  'BQ_HJ_011_3_3': '737355134077779977',
  'BQ_HJ_011_4_1': '737355134077779978',
  'BQ_HJ_011_4_2': '737355134077779979',
  'BQ_HJ_011_4_3': '737355134077779980',
  'BQ_HJ_012_1_1': '737355040217645057',
  'BQ_HJ_012_1_2': '737355040217645058',
  'BQ_HJ_012_1_3': '737355040217645059',
  'BQ_HJ_012_2_1': '737355040217645060',
  'BQ_HJ_012_2_2': '737355040217645061',
  'BQ_HJ_012_2_3': '737355040217645062',
  'BQ_HJ_012_3_1': '737355040217645063',
  'BQ_HJ_012_3_2': '737355040217645064',
  'BQ_HJ_012_3_3': '737355040217645065',
  'BQ_HJ_012_4_1': '737355040217645066',
  'BQ_HJ_012_4_2': '737355040217645067',
  'BQ_HJ_012_4_3': '737355040217645068',
  'BQ_HJ_013_1_1': '737361372756860929',
  'BQ_HJ_013_1_2': '737361372756860930',
  'BQ_HJ_013_1_3': '737361372756860931',
  'BQ_HJ_013_2_1': '737361372756860932',
  'BQ_HJ_013_2_2': '737361372756860933',
  'BQ_HJ_013_2_3': '737361372756860934',
  'BQ_HJ_013_3_1': '737361372756860935',
  'BQ_HJ_013_3_2': '737361372756860936',
  'BQ_HJ_013_3_3': '737361372756860937',
  'BQ_HJ_013_4_1': '737361372756860938',
  'BQ_HJ_013_4_2': '737361372756860939',
  'BQ_HJ_013_4_3': '737361372756860940',
  'CQ_HJ_001_1_1': '737358104102191105',
  'CQ_HJ_001_1_2': '737358104102191106',
  'CQ_HJ_001_1_3': '737358104102191107',
  'CQ_HJ_001_2_1': '737358104102191108',
  'CQ_HJ_001_2_2': '737358104102191109',
  'CQ_HJ_001_2_3': '737358104102191110',
  'CQ_HJ_001_3_1': '737358104102191111',
  'CQ_HJ_001_3_2': '737358104102191112',
  'CQ_HJ_001_3_3': '737358104102191113',
  'CQ_HJ_001_4_1': '737358104102191114',
  'CQ_HJ_001_4_2': '737358104102191115',
  'CQ_HJ_001_4_3': '737358104102191116',
  'CQ_HJ_002_1_1': '737358019582771201',
  'CQ_HJ_002_1_2': '737358019582771202',
  'CQ_HJ_002_1_3': '737358019582771203',
  'CQ_HJ_002_2_1': '737358019582771204',
  'CQ_HJ_002_2_2': '737358019582771205',
  'CQ_HJ_002_2_3': '737358019582771206',
  'CQ_HJ_002_3_1': '737358019582771207',
  'CQ_HJ_002_3_2': '737358019582771208',
  'CQ_HJ_002_3_3': '737358019582771209',
  'CQ_HJ_002_4_1': '737358019582771210',
  'CQ_HJ_002_4_2': '737358019582771211',
  'CQ_HJ_002_4_3': '737358019582771212',
  'CQ_HJ_003_1_1': '737357941375778817',
  'CQ_HJ_003_1_2': '737357941375778818',
  'CQ_HJ_003_1_3': '737357941375778819',
  'CQ_HJ_003_2_1': '737357941375778820',
  'CQ_HJ_003_2_2': '737357941375778821',
  'CQ_HJ_003_2_3': '737357941375778822',
  'CQ_HJ_003_3_1': '737357941375778823',
  'CQ_HJ_003_3_2': '737357941375778824',
  'CQ_HJ_003_3_3': '737357941375778825',
  'CQ_HJ_003_4_1': '737357941375778826',
  'CQ_HJ_003_4_2': '737357941375778827',
  'CQ_HJ_003_4_3': '737357941375778828',
  'CQ_HJ_004_1_1': '737357843107430401',
  'CQ_HJ_004_1_2': '737357843107430402',
  'CQ_HJ_004_1_3': '737357843107430403',
  'CQ_HJ_004_2_1': '737357843107430404',
  'CQ_HJ_004_2_2': '737357843107430405',
  'CQ_HJ_004_2_3': '737357843107430406',
  'CQ_HJ_004_3_1': '737357843107430407',
  'CQ_HJ_004_3_2': '737357843107430408',
  'CQ_HJ_004_3_3': '737357843107430409',
  'CQ_HJ_004_4_1': '737357843107430410',
  'CQ_HJ_004_4_2': '737357843107430411',
  'CQ_HJ_004_4_3': '737357843107430412',
  'CQ_HJ_005_1_1': '737357723695595521',
  'CQ_HJ_005_1_2': '737357723695595522',
  'CQ_HJ_005_1_3': '737357723695595523',
  'CQ_HJ_005_2_1': '737357723695595524',
  'CQ_HJ_005_2_2': '737357723695595525',
  'CQ_HJ_005_2_3': '737357723695595526',
  'CQ_HJ_005_3_1': '737357723695595527',
  'CQ_HJ_005_3_2': '737357723695595528',
  'CQ_HJ_005_3_3': '737357723695595529',
  'CQ_HJ_005_4_1': '737357723695595530',
  'CQ_HJ_005_4_2': '737357723695595531',
  'CQ_HJ_005_4_3': '737357723695595532',
  'CQ_HJ_006_1_1': '737357638509281281',
  'CQ_HJ_006_1_2': '737357638509281282',
  'CQ_HJ_006_1_3': '737357638509281283',
  'CQ_HJ_006_2_1': '737357638509281284',
  'CQ_HJ_006_2_2': '737357638509281285',
  'CQ_HJ_006_2_3': '737357638509281286',
  'CQ_HJ_006_3_1': '737357638509281287',
  'CQ_HJ_006_3_2': '737357638509281288',
  'CQ_HJ_006_3_3': '737357638509281289',
  'CQ_HJ_006_4_1': '737357638509281290',
  'CQ_HJ_006_4_2': '737357638509281291',
  'CQ_HJ_006_4_3': '737357638509281292',
  'CQ_HJ_007_1_1': '737357536323452929',
  'CQ_HJ_007_1_2': '737357536323452930',
  'CQ_HJ_007_1_3': '737357536323452931',
  'CQ_HJ_007_2_1': '737357536323452932',
  'CQ_HJ_007_2_2': '737357536323452933',
  'CQ_HJ_007_2_3': '737357536323452934',
  'CQ_HJ_007_3_1': '737357536323452935',
  'CQ_HJ_007_3_2': '737357536323452936',
  'CQ_HJ_007_3_3': '737357536323452937',
  'CQ_HJ_007_4_1': '737357536323452938',
  'CQ_HJ_007_4_2': '737357536323452939',
  'CQ_HJ_007_4_3': '737357536323452940',
  'CQ_HJ_008_1_1': '737357403024277505',
  'CQ_HJ_008_1_2': '737357403024277506',
  'CQ_HJ_008_1_3': '737357403024277507',
  'CQ_HJ_008_2_1': '737357403024277508',
  'CQ_HJ_008_2_2': '737357403024277509',
  'CQ_HJ_008_2_3': '737357403024277510',
  'CQ_HJ_008_3_1': '737357403024277511',
  'CQ_HJ_008_3_2': '737357403024277512',
  'CQ_HJ_008_3_3': '737357403024277513',
  'CQ_HJ_008_4_1': '737357403024277514',
  'CQ_HJ_008_4_2': '737357403024277515',
  'CQ_HJ_008_4_3': '737357403024277516',
  'CQ_HJ_009_1_1': '737357188967972865',
  'CQ_HJ_009_1_2': '737357188967972866',
  'CQ_HJ_009_1_3': '737357188967972867',
  'CQ_HJ_009_2_1': '737357188967972868',
  'CQ_HJ_009_2_2': '737357188967972869',
  'CQ_HJ_009_2_3': '737357188967972870',
  'CQ_HJ_009_3_1': '737357188967972871',
  'CQ_HJ_009_3_2': '737357188967972872',
  'CQ_HJ_009_3_3': '737357188967972873',
  'CQ_HJ_009_4_1': '737357188967972874',
  'CQ_HJ_009_4_2': '737357188967972875',
  'CQ_HJ_009_4_3': '737357188967972876',
  'CQ_HJ_010_1_1': '737357089223229441',
  'CQ_HJ_010_1_2': '737357089223229442',
  'CQ_HJ_010_1_3': '737357089223229443',
  'CQ_HJ_010_2_1': '737357089223229444',
  'CQ_HJ_010_2_2': '737357089223229445',
  'CQ_HJ_010_2_3': '737357089223229446',
  'CQ_HJ_010_3_1': '737357089223229447',
  'CQ_HJ_010_3_2': '737357089223229448',
  'CQ_HJ_010_3_3': '737357089223229449',
  'CQ_HJ_010_4_1': '737357089223229450',
  'CQ_HJ_010_4_2': '737357089223229451',
  'CQ_HJ_010_4_3': '737357089223229452',
  'CQ_HJ_011_1_1': '737357001629384705',
  'CQ_HJ_011_1_2': '737357001629384706',
  'CQ_HJ_011_1_3': '737357001629384707',
  'CQ_HJ_011_2_1': '737357001629384708',
  'CQ_HJ_011_2_2': '737357001629384709',
  'CQ_HJ_011_2_3': '737357001629384710',
  'CQ_HJ_011_3_1': '737357001629384711',
  'CQ_HJ_011_3_2': '737357001629384712',
  'CQ_HJ_011_3_3': '737357001629384713',
  'CQ_HJ_011_4_1': '737357001629384714',
  'CQ_HJ_011_4_2': '737357001629384715',
  'CQ_HJ_011_4_3': '737357001629384716',
  'CQ_HJ_012_1_1': '737356909459554305',
  'CQ_HJ_012_1_2': '737356909459554306',
  'CQ_HJ_012_1_3': '737356909459554307',
  'CQ_HJ_012_2_1': '737356909459554308',
  'CQ_HJ_012_2_2': '737356909459554309',
  'CQ_HJ_012_2_3': '737356909459554310',
  'CQ_HJ_012_3_1': '737356909459554311',
  'CQ_HJ_012_3_2': '737356909459554312',
  'CQ_HJ_012_3_3': '737356909459554313',
  'CQ_HJ_012_4_1': '737356909459554314',
  'CQ_HJ_012_4_2': '737356909459554315',
  'CQ_HJ_012_4_3': '737356909459554316',
  'CQ_HJ_013_1_1': '737356829620977665',
  'CQ_HJ_013_1_2': '737356829620977666',
  'CQ_HJ_013_1_3': '737356829620977667',
  'CQ_HJ_013_2_1': '737356829620977668',
  'CQ_HJ_013_2_2': '737356829620977669',
  'CQ_HJ_013_2_3': '737356829620977670',
  'CQ_HJ_013_3_1': '737356829620977671',
  'CQ_HJ_013_3_2': '737356829620977672',
  'CQ_HJ_013_3_3': '737356829620977673',
  'CQ_HJ_013_4_1': '737356829620977674',
  'CQ_HJ_013_4_2': '737356829620977675',
  'CQ_HJ_013_4_3': '737356829620977676',
  'DQ_HJ_001_1_1': '737351309665255425',
  'DQ_HJ_001_1_2': '737351309665255426',
  'DQ_HJ_001_1_3': '737351309665255427',
  'DQ_HJ_001_2_1': '737351309665255428',
  'DQ_HJ_001_2_2': '737351309665255429',
  'DQ_HJ_001_2_3': '737351309665255430',
  'DQ_HJ_001_3_1': '737351309665255431',
  'DQ_HJ_001_3_2': '737351309665255432',
  'DQ_HJ_001_3_3': '737351309665255433',
  'DQ_HJ_001_4_1': '737351309665255434',
  'DQ_HJ_001_4_2': '737351309665255435',
  'DQ_HJ_001_4_3': '737351309665255436',
  'DQ_HJ_002_1_1': '737351185358667777',
  'DQ_HJ_002_1_2': '737351185358667778',
  'DQ_HJ_002_1_3': '737351185358667779',
  'DQ_HJ_002_2_1': '737351185358667780',
  'DQ_HJ_002_2_2': '737351185358667781',
  'DQ_HJ_002_2_3': '737351185358667782',
  'DQ_HJ_002_3_1': '737351185358667783',
  'DQ_HJ_002_3_2': '737351185358667784',
  'DQ_HJ_002_3_3': '737351185358667785',
  'DQ_HJ_002_4_1': '737351185358667786',
  'DQ_HJ_002_4_2': '737351185358667787',
  'DQ_HJ_002_4_3': '737351185358667788',
  'DQ_HJ_003_1_1': '737351075597926401',
  'DQ_HJ_003_1_2': '737351075597926402',
  'DQ_HJ_003_1_3': '737351075597926403',
  'DQ_HJ_003_2_1': '737351075597926404',
  'DQ_HJ_003_2_2': '737351075597926405',
  'DQ_HJ_003_2_3': '737351075597926406',
  'DQ_HJ_003_3_1': '737351075597926407',
  'DQ_HJ_003_3_2': '737351075597926408',
  'DQ_HJ_003_3_3': '737351075597926409',
  'DQ_HJ_003_4_1': '737351075597926410',
  'DQ_HJ_003_4_2': '737351075597926411',
  'DQ_HJ_003_4_3': '737351075597926412',
  'DQ_HJ_004_1_1': '737350951991787521',
  'DQ_HJ_004_1_2': '737350951991787522',
  'DQ_HJ_004_1_3': '737350951991787523',
  'DQ_HJ_004_2_1': '737350951991787524',
  'DQ_HJ_004_2_2': '737350951991787525',
  'DQ_HJ_004_2_3': '737350951991787526',
  'DQ_HJ_004_3_1': '737350951991787527',
  'DQ_HJ_004_3_2': '737350951991787528',
  'DQ_HJ_004_3_3': '737350951991787529',
  'DQ_HJ_004_4_1': '737350951991787530',
  'DQ_HJ_004_4_2': '737350951991787531',
  'DQ_HJ_004_4_3': '737350951991787532',
  'DQ_HJ_005_1_1': '737350742553411585',
  'DQ_HJ_005_1_2': '737350742553411586',
  'DQ_HJ_005_1_3': '737350742553411587',
  'DQ_HJ_005_2_1': '737350742553411588',
  'DQ_HJ_005_2_2': '737350742553411589',
  'DQ_HJ_005_2_3': '737350742553411590',
  'DQ_HJ_005_3_1': '737350742553411591',
  'DQ_HJ_005_3_2': '737350742553411592',
  'DQ_HJ_005_3_3': '737350742553411593',
  'DQ_HJ_005_4_1': '737350742553411594',
  'DQ_HJ_005_4_2': '737350742553411595',
  'DQ_HJ_005_4_3': '737350742553411596',
  'DQ_HJ_006_1_1': '737350590426005505',
  'DQ_HJ_006_1_2': '737350590426005506',
  'DQ_HJ_006_1_3': '737350590426005507',
  'DQ_HJ_006_2_1': '737350590426005508',
  'DQ_HJ_006_2_2': '737350590426005509',
  'DQ_HJ_006_2_3': '737350590426005510',
  'DQ_HJ_006_3_1': '737350590426005511',
  'DQ_HJ_006_3_2': '737350590426005512',
  'DQ_HJ_006_3_3': '737350590426005513',
  'DQ_HJ_006_4_1': '737350590426005514',
  'DQ_HJ_006_4_2': '737350590426005515',
  'DQ_HJ_006_4_3': '737350590426005516',
  'DQ_HJ_007_1_1': '737350450491441153',
  'DQ_HJ_007_1_2': '737350450491441154',
  'DQ_HJ_007_1_3': '737350450491441155',
  'DQ_HJ_007_2_1': '737350450491441156',
  'DQ_HJ_007_2_2': '737350450491441157',
  'DQ_HJ_007_2_3': '737350450491441158',
  'DQ_HJ_007_3_1': '737350450491441159',
  'DQ_HJ_007_3_2': '737350450491441160',
  'DQ_HJ_007_3_3': '737350450491441161',
  'DQ_HJ_007_4_1': '737350450491441162',
  'DQ_HJ_007_4_2': '737350450491441163',
  'DQ_HJ_007_4_3': '737350450491441164',
  'DQ_HJ_008_1_1': '737350255028486145',
  'DQ_HJ_008_1_2': '737350255028486146',
  'DQ_HJ_008_1_3': '737350255028486147',
  'DQ_HJ_008_2_1': '737350255028486148',
  'DQ_HJ_008_2_2': '737350255028486149',
  'DQ_HJ_008_2_3': '737350255028486150',
  'DQ_HJ_008_3_1': '737350255028486151',
  'DQ_HJ_008_3_2': '737350255028486152',
  'DQ_HJ_008_3_3': '737350255028486153',
  'DQ_HJ_008_4_1': '737350255028486154',
  'DQ_HJ_008_4_2': '737350255028486155',
  'DQ_HJ_008_4_3': '737350255028486156',
  'DQ_HJ_009_1_1': '737350068428095489',
  'DQ_HJ_009_1_2': '737350068428095490',
  'DQ_HJ_009_1_3': '737350068428095491',
  'DQ_HJ_009_2_1': '737350068428095492',
  'DQ_HJ_009_2_2': '737350068428095493',
  'DQ_HJ_009_2_3': '737350068428095494',
  'DQ_HJ_009_3_1': '737350068428095495',
  'DQ_HJ_009_3_2': '737350068428095496',
  'DQ_HJ_009_3_3': '737350068428095497',
  'DQ_HJ_009_4_1': '737350068428095498',
  'DQ_HJ_009_4_2': '737350068428095499',
  'DQ_HJ_009_4_3': '737350068428095500',
  'DQ_HJ_010_1_1': '737348418758000641',
  'DQ_HJ_010_1_2': '737348418758000642',
  'DQ_HJ_010_1_3': '737348418758000643',
  'DQ_HJ_010_2_1': '737348418758000644',
  'DQ_HJ_010_2_2': '737348418758000645',
  'DQ_HJ_010_2_3': '737348418758000646',
  'DQ_HJ_010_3_1': '737348418758000647',
  'DQ_HJ_010_3_2': '737348418758000648',
  'DQ_HJ_010_3_3': '737348418758000649',
  'DQ_HJ_010_4_1': '737348418758000650',
  'DQ_HJ_010_4_2': '737348418758000651',
  'DQ_HJ_010_4_3': '737348418758000652',
  'DQ_HJ_011_1_1': '737343395927957505',
  'DQ_HJ_011_1_2': '737343395927957506',
  'DQ_HJ_011_1_3': '737343395927957507',
  'DQ_HJ_011_2_1': '737343395927957508',
  'DQ_HJ_011_2_2': '737343395927957509',
  'DQ_HJ_011_2_3': '737343395927957510',
  'DQ_HJ_011_3_1': '737343395927957511',
  'DQ_HJ_011_3_2': '737343395927957512',
  'DQ_HJ_011_3_3': '737343395927957513',
  'DQ_HJ_011_4_1': '737343395927957514',
  'DQ_HJ_011_4_2': '737343395927957515',
  'DQ_HJ_011_4_3': '737343395927957516',

  // F区 (公物仓二)
  'FQ_HJ_001_1_1': '737358799526182913',
  'FQ_HJ_001_1_2': '737358799526182914',
  'FQ_HJ_001_1_3': '737358799526182915',
  'FQ_HJ_001_2_1': '737358799526182916',
  'FQ_HJ_001_2_2': '737358799526182917',
  'FQ_HJ_001_2_3': '737358799526182918',
  'FQ_HJ_001_3_1': '737358799526182919',
  'FQ_HJ_001_3_2': '737358799526182920',
  'FQ_HJ_001_3_3': '737358799526182921',
  'FQ_HJ_001_4_1': '737358799526182922',
  'FQ_HJ_001_4_2': '737358799526182923',
  'FQ_HJ_001_4_3': '737358799526182924',
  'FQ_HJ_002_1_1': '737358876718153729',
  'FQ_HJ_002_1_2': '737358876718153730',
  'FQ_HJ_002_1_3': '737358876718153731',
  'FQ_HJ_002_2_1': '737358876718153732',
  'FQ_HJ_002_2_2': '737358876718153733',
  'FQ_HJ_002_2_3': '737358876718153734',
  'FQ_HJ_002_3_1': '737358876718153735',
  'FQ_HJ_002_3_2': '737358876718153736',
  'FQ_HJ_002_3_3': '737358876718153737',
  'FQ_HJ_002_4_1': '737358876718153738',
  'FQ_HJ_002_4_2': '737358876718153739',
  'FQ_HJ_002_4_3': '737358876718153740',
  'FQ_HJ_003_1_1': '737358954132422657',
  'FQ_HJ_003_1_2': '737358954132422658',
  'FQ_HJ_003_1_3': '737358954132422659',
  'FQ_HJ_003_2_1': '737358954132422660',
  'FQ_HJ_003_2_2': '737358954132422661',
  'FQ_HJ_003_2_3': '737358954132422662',
  'FQ_HJ_003_3_1': '737358954132422663',
  'FQ_HJ_003_3_2': '737358954132422664',
  'FQ_HJ_003_3_3': '737358954132422665',
  'FQ_HJ_003_4_1': '737358954132422666',
  'FQ_HJ_003_4_2': '737358954132422667',
  'FQ_HJ_003_4_3': '737358954132422668',
  'FQ_HJ_004_1_1': '737359052392382465',
  'FQ_HJ_004_1_2': '737359052392382466',
  'FQ_HJ_004_1_3': '737359052392382467',
  'FQ_HJ_004_2_1': '737359052392382468',
  'FQ_HJ_004_2_2': '737359052392382469',
  'FQ_HJ_004_2_3': '737359052392382470',
  'FQ_HJ_004_3_1': '737359052392382471',
  'FQ_HJ_004_3_2': '737359052392382472',
  'FQ_HJ_004_3_3': '737359052392382473',
  'FQ_HJ_004_4_1': '737359052392382474',
  'FQ_HJ_004_4_2': '737359052392382475',
  'FQ_HJ_004_4_3': '737359052392382476',
  'FQ_HJ_005_1_1': '737359147561140225',
  'FQ_HJ_005_1_2': '737359147561140226',
  'FQ_HJ_005_1_3': '737359147561140227',
  'FQ_HJ_005_2_1': '737359147561140228',
  'FQ_HJ_005_2_2': '737359147561140229',
  'FQ_HJ_005_2_3': '737359147561140230',
  'FQ_HJ_005_3_1': '737359147561140231',
  'FQ_HJ_005_3_2': '737359147561140232',
  'FQ_HJ_005_3_3': '737359147561140233',
  'FQ_HJ_005_4_1': '737359147561140234',
  'FQ_HJ_005_4_2': '737359147561140235',
  'FQ_HJ_005_4_3': '737359147561140236',
  'FQ_HJ_006_1_1': '737359226841874433',
  'FQ_HJ_006_1_2': '737359226841874434',
  'FQ_HJ_006_1_3': '737359226841874435',
  'FQ_HJ_006_2_1': '737359226841874436',
  'FQ_HJ_006_2_2': '737359226841874437',
  'FQ_HJ_006_2_3': '737359226841874438',
  'FQ_HJ_006_3_1': '737359226841874439',
  'FQ_HJ_006_3_2': '737359226841874440',
  'FQ_HJ_006_3_3': '737359226841874441',
  'FQ_HJ_006_4_1': '737359226841874442',
  'FQ_HJ_006_4_2': '737359226841874443',
  'FQ_HJ_006_4_3': '737359226841874444',
  'FQ_HJ_007_1_1': '737359306596564993',
  'FQ_HJ_007_1_2': '737359306596564994',
  'FQ_HJ_007_1_3': '737359306596564995',
  'FQ_HJ_007_2_1': '737359306596564996',
  'FQ_HJ_007_2_2': '737359306596564997',
  'FQ_HJ_007_2_3': '737359306596564998',
  'FQ_HJ_007_3_1': '737359306596564999',
  'FQ_HJ_007_3_2': '737359306596565000',
  'FQ_HJ_007_3_3': '737359306596565001',
  'FQ_HJ_007_4_1': '737359306596565002',
  'FQ_HJ_007_4_2': '737359306596565003',
  'FQ_HJ_007_4_3': '737359306596565004',
  'FQ_HJ_008_1_1': '737359393624178689',
  'FQ_HJ_008_1_2': '737359393624178690',
  'FQ_HJ_008_1_3': '737359393624178691',
  'FQ_HJ_008_2_1': '737359393624178692',
  'FQ_HJ_008_2_2': '737359393624178693',
  'FQ_HJ_008_2_3': '737359393624178694',
  'FQ_HJ_008_3_1': '737359393624178695',
  'FQ_HJ_008_3_2': '737359393624178696',
  'FQ_HJ_008_3_3': '737359393624178697',
  'FQ_HJ_008_4_1': '737359393624178698',
  'FQ_HJ_008_4_2': '737359393624178699',
  'FQ_HJ_008_4_3': '737359393624178700',

};

// 反向映射：从真实ID到库位编号
const idToLocationMapping: Record<string, string> = {};

// 初始化反向映射
Object.entries(locationIdMapping).forEach(([locationCode, id]) => {
  idToLocationMapping[id] = locationCode;
});

// 辅助函数：将库位编号转换为货架ID、层ID和位置ID
const parseLocationCode = (locationCode: string): { warehouseId: string, shelfId: string, layerId: string, positionId: string } | null => {
  if (!locationCode) return null;

  const parts = locationCode.split('_');
  if (parts.length < 5) return null;

  const areaCode = parts[0]; // 如 "AQ"
  const shelfType = parts[1]; // 如 "HJ"
  const shelfNum = parts[2]; // 如 "001"
  const layer = parts[3]; // 如 "1"
  const position = parts[4]; // 如 "1"

  // 确定仓库ID
  const warehouseId = ['AQ', 'BQ', 'CQ', 'DQ'].includes(areaCode) ? 'warehouse1' : 'warehouse2';

  // 构建货架ID (如 "A1")
  const area = areaCode.charAt(0);
  const shelfId = `${area}${parseInt(shelfNum, 10)}`;

  // 构建层ID和位置ID
  const layerId = `${shelfId}-L${layer}`;
  const positionId = `${layerId}-P${position}`;

  return { warehouseId, shelfId, layerId, positionId };
};

// 辅助函数：将货架ID、层ID和位置ID转换为库位编号
const buildLocationCode = (shelfId: string, layerId: string, positionId: string): string | null => {
  if (!shelfId || !layerId || !positionId) return null;

  // 从货架ID提取区域代码 (如 "A" -> "AQ")
  const area = shelfId.charAt(0);
  const areaCode = `${area}Q`;

  // 从货架ID提取货架编号 (如 "A1" -> "001")
  const shelfNum = shelfId.substring(1);
  const paddedShelfNum = shelfNum.padStart(3, '0');

  // 从层ID和位置ID提取层号和位置号
  const layerMatch = layerId.match(/L(\d+)$/);
  const positionMatch = positionId.match(/P(\d+)$/);

  if (!layerMatch || !positionMatch) return null;

  const layer = layerMatch[1];
  const position = positionMatch[1];

  // 构建库位编号 (如 "AQ_HJ_001_1_1")
  return `${areaCode}_HJ_${paddedShelfNum}_${layer}_${position}`;
};

// 模拟API响应数据 - 公物仓一
const mockWarehouseData1: WarehouseMap = {
  id: 'warehouse1',
  name: '公物仓（一）',
  width: 800,
  height: 600,
  shelves: {
    a: [
      { id: 'A13', name: 'A13', itemCount: 2, status: 'empty' },
      { id: 'A12', name: 'A12', itemCount: 9, status: 'empty' },
      { id: 'A11', name: 'A11', itemCount: 8, status: 'empty' },
      { id: 'A10', name: 'A10', itemCount: 4, status: 'empty' },
      { id: 'A9', name: 'A9', itemCount: 11, status: 'empty' },
      { id: 'A8', name: 'A8', itemCount: 6, status: 'empty' },
      { id: 'A7', name: 'A7', itemCount: 9, status: 'empty' },
      { id: 'A6', name: 'A6', itemCount: 7, status: 'empty' },
      { id: 'A5', name: 'A5', itemCount: 3, status: 'empty' },
      { id: 'A4', name: 'A4', itemCount: 10, status: 'empty' },
      { id: 'A3', name: 'A3', itemCount: 5, status: 'empty' },
      { id: 'A2', name: 'A2', itemCount: 12, status: 'empty' },
      { id: 'A1', name: 'A1', itemCount: 8, status: 'empty' },
    ],
    b: [
      { id: 'B13', name: 'B13', itemCount: 5, status: 'empty' },
      { id: 'B12', name: 'B12', itemCount: 8, status: 'empty' },
      { id: 'B11', name: 'B11', itemCount: 7, status: 'empty' },
      { id: 'B10', name: 'B10', itemCount: 3, status: 'empty' },
      { id: 'B9', name: 'B9', itemCount: 11, status: 'empty' },
      { id: 'B8', name: 'B8', itemCount: 6, status: 'empty' },
      { id: 'B7', name: 'B7', itemCount: 9, status: 'empty' },
      { id: 'B6', name: 'B6', itemCount: 12, status: 'empty' },
      { id: 'B5', name: 'B5', itemCount: 4, status: 'empty' },
      { id: 'B4', name: 'B4', itemCount: 8, status: 'empty' },
      { id: 'B3', name: 'B3', itemCount: 10, status: 'empty' },
      { id: 'B2', name: 'B2', itemCount: 5, status: 'empty' },
      { id: 'B1', name: 'B1', itemCount: 7, status: 'empty' },
    ],
    c: [
      { id: 'C13', name: 'C13', itemCount: 7, status: 'empty' },
      { id: 'C12', name: 'C12', itemCount: 4, status: 'empty' },
      { id: 'C11', name: 'C11', itemCount: 9, status: 'empty' },
      { id: 'C10', name: 'C10', itemCount: 6, status: 'empty' },
      { id: 'C9', name: 'C9', itemCount: 12, status: 'empty' },
      { id: 'C8', name: 'C8', itemCount: 5, status: 'empty' },
      { id: 'C7', name: 'C7', itemCount: 8, status: 'empty' },
      { id: 'C6', name: 'C6', itemCount: 10, status: 'empty' },
      { id: 'C5', name: 'C5', itemCount: 3, status: 'empty' },
      { id: 'C4', name: 'C4', itemCount: 7, status: 'empty' },
      { id: 'C3', name: 'C3', itemCount: 11, status: 'empty' },
      { id: 'C2', name: 'C2', itemCount: 4, status: 'empty' },
      { id: 'C1', name: 'C1', itemCount: 9, status: 'empty' },
    ],
    d: [
      { id: 'D11', name: 'D11', itemCount: 9, status: 'empty' },
      { id: 'D10', name: 'D10', itemCount: 5, status: 'empty' },
      { id: 'D9', name: 'D9', itemCount: 8, status: 'empty' },
      { id: 'D8', name: 'D8', itemCount: 11, status: 'empty' },
      { id: 'D7', name: 'D7', itemCount: 4, status: 'empty' },
      { id: 'D6', name: 'D6', itemCount: 7, status: 'empty' },
      { id: 'D5', name: 'D5', itemCount: 10, status: 'empty' },
      { id: 'D4', name: 'D4', itemCount: 5, status: 'empty' },
      { id: 'D3', name: 'D3', itemCount: 9, status: 'empty' },
      { id: 'D2', name: 'D2', itemCount: 3, status: 'empty' },
      { id: 'D1', name: 'D1', itemCount: 6, status: 'empty' },
    ]
  }
};

// 模拟API响应数据 - 公物仓二
const mockWarehouseData2: WarehouseMap = {
  id: 'warehouse2',
  name: '公物仓（二）',
  width: 800,
  height: 600,
  shelves: {
    // 必需的字段，但为空数组
    a: [],
    b: [],
    c: [],
    d: [],
    // 顶部横向排列的E区货架 - 已全部移除
    e: [],
    // 右侧F区货架 - 从上到下F5、F6、F7、F8
    f: [
      { id: 'F5', name: 'F5', itemCount: 8, status: 'empty' },
      { id: 'F6', name: 'F6', itemCount: 8, status: 'empty' },
      { id: 'F7', name: 'F7', itemCount: 8, status: 'empty' },
      { id: 'F8', name: 'F8', itemCount: 8, status: 'empty' },
    ],
    // 左侧货架 - 从上到下F1、F2、F3、F4
    r: [
      { id: 'F1', name: 'F1', itemCount: 5, status: 'empty' },
      { id: 'F2', name: 'F2', itemCount: 5, status: 'empty' },
      { id: 'F3', name: 'F3', itemCount: 7, status: 'empty' },
      { id: 'F4', name: 'F4', itemCount: 10, status: 'empty' },
    ],
  }
};

// 所有仓库的集合
const allWarehouses: Record<string, WarehouseMap> = {
  warehouse1: mockWarehouseData1,
  warehouse2: mockWarehouseData2
};

// 为各仓库生成物品数据
const allWarehouseItemsData: Record<string, Record<string, Record<string, Record<string, WarehouseItem[]>>>> = {
  warehouse1: {},
  warehouse2: {}
};

// 模拟货架详情数据
const generateShelfDetail = (warehouseId: string, shelfId: string): ShelfDetail => {
  const shelfItemsData = allWarehouseItemsData[warehouseId]?.[shelfId] || {};

  const layers = [1, 2, 3, 4].map(layerNum => {
    const layerId = `${shelfId}-L${layerNum}`;

    return {
      id: layerId,
      positions: [1, 2, 3].map(posNum => {
        const positionId = `${shelfId}-L${layerNum}-P${posNum}`;
        const hasItems = shelfItemsData[layerId] &&
          shelfItemsData[layerId][positionId] &&
          shelfItemsData[layerId][positionId].length > 0;

        return {
          id: positionId,
          name: `${posNum}`,
          hasItems
        };
      })
    };
  });

  return {
    id: shelfId,
    name: shelfId,
    layers
  };
};

// 生成固定的物品数据（不再随机）
const generateItemsForShelf = (warehouseId: string, shelfId: string): Record<string, Record<string, WarehouseItem[]>> => {
  const result: Record<string, Record<string, WarehouseItem[]>> = {};

  // 默认所有层和库位都无物品
  for (let layerNum = 1; layerNum <= 4; layerNum++) {
    const layerId = `${shelfId}-L${layerNum}`;
    result[layerId] = {};

    for (let posNum = 1; posNum <= 3; posNum++) {
      const positionId = `${shelfId}-L${layerNum}-P${posNum}`;
      result[layerId][positionId] = [];
    }
  }

  // 不再添加任何物品，所有货架都为空
  return result;
};

// 辅助函数：向指定层的指定库位添加物品
const addItemsToLayer = (
  result: Record<string, Record<string, WarehouseItem[]>>,
  shelfId: string,
  layerNum: number,
  itemCount: number
) => {
  const layerId = `${shelfId}-L${layerNum}`;
  const firstChar = shelfId.charAt(0);

  // 根据不同区域设置不同的物品类别
  let category = '电子';
  if (['A', 'E'].includes(firstChar)) category = '电子';
  else if (['B', 'F'].includes(firstChar)) category = '办公';
  else if (['C', 'R'].includes(firstChar)) category = '工具';
  else if (firstChar === 'D') category = '包装';

  // 由于我们不再添加物品，此函数实际上不会被调用
  // 但为了代码完整性，仍然保留此函数
  for (let posNum = 1; posNum <= 3; posNum++) {
    const positionId = `${shelfId}-L${layerNum}-P${posNum}`;
    result[layerId][positionId] = []; // 确保库位为空
  }
};

// 为A、B、C、D区所有货架生成物品 (公物仓一)
Object.entries(mockWarehouseData1.shelves).forEach(([area, shelves]) => {
  allWarehouseItemsData.warehouse1[area] = {};
  shelves.forEach(shelf => {
    allWarehouseItemsData.warehouse1[shelf.id] = generateItemsForShelf('warehouse1', shelf.id);
  });
});

// 为E、F、R区所有货架生成物品 (公物仓二)
Object.entries(mockWarehouseData2.shelves).forEach(([area, shelves]) => {
  allWarehouseItemsData.warehouse2[area] = {};
  shelves.forEach(shelf => {
    allWarehouseItemsData.warehouse2[shelf.id] = generateItemsForShelf('warehouse2', shelf.id);
  });
});

// 更新货架状态，根据有物品的层数 - 由于已经没有物品，这个函数会将所有货架设为empty
const updateShelfStatuses = (warehouseId: string) => {
  const warehouseData = allWarehouses[warehouseId];
  if (!warehouseData) return;

  Object.entries(warehouseData.shelves).forEach(([area, shelves]) => {
    shelves.forEach((shelf: Shelf) => {
      // 直接设置所有货架状态为empty
      shelf.status = 'empty'; // 没有层有物品 - 灰色
    });
  });
};

// 调用更新函数，更新两个仓库的状态
updateShelfStatuses('warehouse1');
updateShelfStatuses('warehouse2');

// 获取所有仓库列表
export const fetchWarehouseList = (): Promise<{ id: string; name: string }[]> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(Object.values(allWarehouses).map(warehouse => ({
        id: warehouse.id,
        name: warehouse.name
      })));
    }, 300);
  });
};

// 获取仓库地图数据
export const fetchWarehouseMap = (warehouseId: string = 'warehouse1'): Promise<WarehouseMap> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(allWarehouses[warehouseId] || mockWarehouseData1);
    }, 500);
  });
};

// 获取货架详细信息
export const fetchShelfDetail = (warehouseId: string = 'warehouse1', shelfId: string): Promise<ShelfDetail> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      try {
        const shelfDetail = generateShelfDetail(warehouseId, shelfId);

        // 为每个库位添加真实ID
        shelfDetail.layers.forEach(layer => {
          layer.positions.forEach(position => {
            try {
              // 构建库位编号
              const locationCode = buildLocationCode(shelfId, layer.id, position.id);
              if (locationCode && locationIdMapping[locationCode]) {
                // 添加真实ID到库位信息中
                position.realId = locationIdMapping[locationCode];
              }
            } catch (posError) {
              console.error(`处理库位 ${position.id} 时出错:`, posError);
              // 确保position.hasItems有一个默认值
              position.hasItems = false;
            }
          });
        });

        resolve(shelfDetail);
      } catch (error) {
        console.error(`获取货架 ${shelfId} 详情时出错:`, error);
        // 返回一个空的货架详情，避免UI崩溃
        const emptyShelfDetail: ShelfDetail = {
          id: shelfId,
          name: shelfId,
          layers: [1, 2, 3, 4].map(layerNum => ({
            id: `${shelfId}-L${layerNum}`,
            positions: [1, 2, 3].map(posNum => ({
              id: `${shelfId}-L${layerNum}-P${posNum}`,
              name: `${posNum}`,
              hasItems: false
            }))
          }))
        };
        resolve(emptyShelfDetail);
      }
    }, 300);
  });
};

// 获取货架层中特定位置的物品
export const fetchPositionItems = (
  warehouseId: string = 'warehouse1',
  shelfId: string,
  layerId: string,
  positionId: string
): Promise<WarehouseItem[]> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      try {
        let items: WarehouseItem[] = [];

        if (
          allWarehouseItemsData[warehouseId] &&
          allWarehouseItemsData[warehouseId][shelfId] &&
          allWarehouseItemsData[warehouseId][shelfId][layerId] &&
          allWarehouseItemsData[warehouseId][shelfId][layerId][positionId]
        ) {
          items = allWarehouseItemsData[warehouseId][shelfId][layerId][positionId];

          // 为每个物品添加真实库位ID
          const locationCode = buildLocationCode(shelfId, layerId, positionId);
          if (locationCode && locationIdMapping[locationCode]) {
            const realId = locationIdMapping[locationCode];
            items = items.map(item => ({
              ...item,
              realLocationId: realId,
              locationCode: locationCode
            }));
          }

          // 验证每个物品的必要属性
          items = items.filter(isValidWarehouseItem);
        }

        resolve(items);
      } catch (error) {
        console.error(`获取位置 ${positionId} 的物品时出错:`, error);
        // 返回空数组，避免UI崩溃
        resolve([]);
      }
    }, 300);
  });
};

// 根据真实ID查找库位信息
export const findLocationByRealId = (realId: string): Promise<{
  found: boolean;
  warehouseId?: string;
  shelfId?: string;
  layerId?: string;
  positionId?: string;
  locationCode?: string;
}> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      const locationCode = idToLocationMapping[realId];

      if (locationCode) {
        const locationInfo = parseLocationCode(locationCode);

        if (locationInfo) {
          resolve({
            found: true,
            warehouseId: locationInfo.warehouseId,
            shelfId: locationInfo.shelfId,
            layerId: locationInfo.layerId,
            positionId: locationInfo.positionId,
            locationCode
          });
          return;
        }
      }

      resolve({ found: false });
    }, 300);
  });
};

// 添加处理物品数据的方法
// 解析物品数据中的库位ID信息
const parseLocationId = (item: Record<string, any>): string | null => {
  try {
    // 直接查找属性 "T710063228176306177"
    if (item && item.T710063228176306177 && typeof item.T710063228176306177 === 'string') {
      // 如果数据以S开头，则移除S并返回剩余部分
      if (item.T710063228176306177.startsWith('S')) {
        return item.T710063228176306177.substring(1);
      }
      return item.T710063228176306177;
    }

    // 尝试检查是否有data属性
    if (item && item.data) {
      if (typeof item.data === 'object' && item.data !== null) {
        if (item.data.T710063228176306177 && typeof item.data.T710063228176306177 === 'string') {
          if (item.data.T710063228176306177.startsWith('S')) {
            return item.data.T710063228176306177.substring(1);
          }
          return item.data.T710063228176306177;
        }
      } else if (typeof item.data === 'string') {
        // 尝试解析JSON字符串
        try {
          const parsedData = JSON.parse(item.data);
          if (parsedData && typeof parsedData === 'object') {
            if (parsedData.T710063228176306177 && typeof parsedData.T710063228176306177 === 'string') {
              if (parsedData.T710063228176306177.startsWith('S')) {
                return parsedData.T710063228176306177.substring(1);
              }
              return parsedData.T710063228176306177;
            }
          }
        } catch (e) {
          // 解析失败，忽略
        }
      }
    }

    // 查找名称包含目标ID的属性
    for (const key in item) {
      if (typeof key === 'string' && key.includes('710063228176306177')) {
        const value = item[key];
        if (typeof value === 'string') {
          if (value.startsWith('S')) {
            return value.substring(1);
          }
          return value;
        }
      }
    }

    // 检查item.attributes或其他可能的容器属性
    const containerProps = ['attributes', 'properties', 'fields', 'values', 'metadata'];
    for (const prop of containerProps) {
      if (item[prop] && typeof item[prop] === 'object') {
        // 直接在容器中查找
        if (item[prop].T710063228176306177 && typeof item[prop].T710063228176306177 === 'string') {
          const value = item[prop].T710063228176306177;
          if (value.startsWith('S')) {
            return value.substring(1);
          }
          return value;
        }

        // 在容器中查找匹配的键
        for (const key in item[prop]) {
          if (typeof key === 'string' && key.includes('710063228176306177')) {
            const value = item[prop][key];
            if (typeof value === 'string') {
              if (value.startsWith('S')) {
                return value.substring(1);
              }
              return value;
            }
          }
        }
      }
    }

    // 最后尝试递归搜索（限制深度）
    const searchInObject = (obj: any, depth = 0): string | null => {
      if (!obj || typeof obj !== 'object' || depth > 2) return null;

      // 先查找直接匹配的键
      for (const key in obj) {
        if (typeof key === 'string' && key.includes('710063228176306177')) {
          const value = obj[key];
          if (typeof value === 'string') {
            if (value.startsWith('S')) {
              return value.substring(1);
            }
            return value;
          }
        }
      }

      // 然后递归搜索子对象
      for (const key in obj) {
        if (obj[key] && typeof obj[key] === 'object') {
          const result = searchInObject(obj[key], depth + 1);
          if (result) return result;
        }
      }

      return null;
    };

    return searchInObject(item);
  } catch (error) {
    console.error('解析库位ID时出错:', error);
    return null;
  }
};

// 获取物品名称
const getItemName = (item: Record<string, any>): string => {
  try {
    // 目标属性ID
    const TARGET_ID = '527635710128300032';

    // 直接查找属性
    if (item && item.T527635710128300032) {
      return String(item.T527635710128300032);
    }

    // 查找名称包含目标ID的属性
    for (const key in item) {
      if (typeof key === 'string' && key.includes(TARGET_ID)) {
        return String(item[key]);
      }
    }

    // 检查容器属性和递归搜索
    const searchNameInObject = (obj: any, depth = 0): string | null => {
      if (!obj || typeof obj !== 'object' || depth > 2) return null;

      // 如果是data对象，直接查找
      if (obj.T527635710128300032) {
        return String(obj.T527635710128300032);
      }

      // 查找匹配的键
      for (const key in obj) {
        if (typeof key === 'string' && key.includes(TARGET_ID)) {
          return String(obj[key]);
        }

        // 递归搜索子对象
        if (obj[key] && typeof obj[key] === 'object') {
          const result = searchNameInObject(obj[key], depth + 1);
          if (result) return result;
        }
      }

      return null;
    };

    // 检查data属性
    if (item.data) {
      const nameFromData = searchNameInObject(
        typeof item.data === 'string' ? JSON.parse(item.data) : item.data
      );
      if (nameFromData) return nameFromData;
    }

    // 检查其他常见容器属性
    const containers = ['attributes', 'properties', 'fields', 'values', 'metadata'];
    for (const prop of containers) {
      if (item[prop]) {
        const nameFromContainer = searchNameInObject(item[prop]);
        if (nameFromContainer) return nameFromContainer;
      }
    }

    // 递归搜索整个对象
    const foundName = searchNameInObject(item);
    if (foundName) return foundName;

    return '未知物品';
  } catch (error) {
    return '未知物品';
  }
};

// 获取物品卡片编码
const getItemCardCode = (item: Record<string, any>): string => {
  try {
    // 目标属性ID
    const TARGET_ID = '527635705355182080';

    // 直接查找属性
    if (item && item.T527635705355182080) {
      return String(item.T527635705355182080);
    }

    // 查找名称包含目标ID的属性
    for (const key in item) {
      if (typeof key === 'string' && key.includes(TARGET_ID)) {
        return String(item[key]);
      }
    }

    // 检查容器属性和递归搜索
    const searchCodeInObject = (obj: any, depth = 0): string | null => {
      if (!obj || typeof obj !== 'object' || depth > 2) return null;

      // 如果是data对象，直接查找
      if (obj.T527635705355182080) {
        return String(obj.T527635705355182080);
      }

      // 查找匹配的键
      for (const key in obj) {
        if (typeof key === 'string' && key.includes(TARGET_ID)) {
          return String(obj[key]);
        }

        // 递归搜索子对象
        if (obj[key] && typeof obj[key] === 'object') {
          const result = searchCodeInObject(obj[key], depth + 1);
          if (result) return result;
        }
      }

      return null;
    };

    // 检查data属性
    if (item.data) {
      const codeFromData = searchCodeInObject(
        typeof item.data === 'string' ? JSON.parse(item.data) : item.data
      );
      if (codeFromData) return codeFromData;
    }

    // 检查其他常见容器属性
    const containers = ['attributes', 'properties', 'fields', 'values', 'metadata'];
    for (const prop of containers) {
      if (item[prop]) {
        const codeFromContainer = searchCodeInObject(item[prop]);
        if (codeFromContainer) return codeFromContainer;
      }
    }

    // 递归搜索整个对象
    const foundCode = searchCodeInObject(item);
    if (foundCode) return foundCode;

    return '无编码';
  } catch (error) {
    return '无编码';
  }
};

// 获取物品取得日期
const getItemAcquiredDate = (item: Record<string, any>): string => {
  try {
    // 目标属性ID
    const TARGET_ID = '527635558076391424';

    // 直接查找属性
    if (item && item.T527635558076391424) {
      return String(item.T527635558076391424);
    }

    // 查找名称包含目标ID的属性
    for (const key in item) {
      if (typeof key === 'string' && key.includes(TARGET_ID)) {
        return String(item[key]);
      }
    }

    // 检查容器属性和递归搜索
    const searchDateInObject = (obj: any, depth = 0): string | null => {
      if (!obj || typeof obj !== 'object' || depth > 2) return null;

      // 如果是data对象，直接查找
      if (obj.T527635558076391424) {
        return String(obj.T527635558076391424);
      }

      // 查找匹配的键
      for (const key in obj) {
        if (typeof key === 'string' && key.includes(TARGET_ID)) {
          return String(obj[key]);
        }

        // 递归搜索子对象
        if (obj[key] && typeof obj[key] === 'object') {
          const result = searchDateInObject(obj[key], depth + 1);
          if (result) return result;
        }
      }

      return null;
    };

    // 检查data属性
    if (item.data) {
      const dateFromData = searchDateInObject(
        typeof item.data === 'string' ? JSON.parse(item.data) : item.data
      );
      if (dateFromData) return dateFromData;
    }

    // 检查其他常见容器属性
    const containers = ['attributes', 'properties', 'fields', 'values', 'metadata'];
    for (const prop of containers) {
      if (item[prop]) {
        const dateFromContainer = searchDateInObject(item[prop]);
        if (dateFromContainer) return dateFromContainer;
      }
    }

    // 递归搜索整个对象
    const foundDate = searchDateInObject(item);
    if (foundDate) return foundDate;

    return '未知日期';
  } catch (error) {
    return '未知日期';
  }
};

// 规范化ID格式，确保能够与locationIdMapping匹配
const normalizeLocationId = (id: string): string => {
  // 去除可能的前缀S
  if (id.startsWith('S')) {
    id = id.substring(1);
  }

  // 去除可能的其他前缀（如T等）
  if (/^[A-Za-z]+\d+$/.test(id)) {
    id = id.replace(/^[A-Za-z]+/, '');
  }

  // 处理可能的格式差异，如短横线、下划线等
  id = id.replace(/[-_]/g, '');

  // 如果ID包含非数字字符，可能是另一种格式，尝试提取数字部分
  if (/\D/.test(id)) {
    const match = id.match(/(\d+)/);
    if (match) {
      id = match[1];
    }
  }

  return id;
};

// 在添加物品数据前，确保所有必要的数据都是有效的
const isValidWarehouseItem = (item: WarehouseItem): boolean => {
  return (
    !!item.id &&
    !!item.shelfId &&
    !!item.layerId &&
    !!item.positionId
  );
};

// 创建安全的物品数据
const createSafeWarehouseItem = (
  item: Record<string, any>,
  shelfId: string,
  layerId: string,
  positionId: string,
  index: number,
  locationCode: string,
  realId: string
): WarehouseItem => {
  return {
    id: `item-${shelfId}-${layerId}-${positionId}-${index}`,
    name: getItemName(item) || '未知物品',
    shelfId,
    layerId,
    positionId,
    quantity: 1,
    sku: getItemCardCode(item) || '无编码',
    category: '公物资产',
    status: 'normal',
    expiryDate: getItemAcquiredDate(item) || '未知日期',
    realLocationId: realId,
    locationCode,
    // 保存原始数据，以便在详情页展示
    originalData: item
  };
};

/**
 * 刷新物品数据并更新库位状态
 * 
 * 功能说明：
 * 1. 调用后端API获取所有物品数据
 * 2. 从返回数据中查找"T736603052777414658"属性，解析库位信息（如"二号仓库/F1/库位 1_1"）
 * 3. 将库位信息转换为对应的库位编号（如"FQ_HJ_001_1_1"）
 * 4. 更新匹配库位的物品数据，提取以下信息：
 *    - "T527635705355182080"后的数据表示卡片编码号
 *    - "T527635710128300032"后的数据表示物品名称
 *    - "T527635558076391424"后的数据表示取得日期
 * 5. 更新货架状态：有物品的库位显示为蓝色，其他为灰色
 * 6. 根据每个货架上有物品的层数，更新货架颜色：
 *    - 0层：灰色 (empty)
 *    - 1层：蓝色 (first)
 *    - 2层：绿色 (second)
 *    - 3层：黄色 (third)
 *    - 4层：红色 (fourth)
 * 
 * 使用方法：在点击刷新按钮时调用此函数
 */
export const refreshItemsData = async (): Promise<boolean> => {
  try {
    console.log('开始刷新物品数据...');

    // 调用auth.ts中的getAllItems方法获取所有物品数据
    const result = await authService.getAllItems();

    if (!result.success) {
      console.error('获取物品数据失败:', result.msg);
      return false;
    }

    // 重置所有货架的状态为空
    try {
      // 清空所有货架的物品数据 - 先执行清空操作，确保即使后续解析失败也不会导致UI错误
      Object.entries(mockWarehouseData1.shelves).forEach(([area, shelves]) => {
        shelves.forEach(shelf => {
          allWarehouseItemsData.warehouse1[shelf.id] = generateItemsForShelf('warehouse1', shelf.id);
          // 确保所有货架状态为empty
          shelf.status = 'empty';
        });
      });

      Object.entries(mockWarehouseData2.shelves).forEach(([area, shelves]) => {
        shelves.forEach(shelf => {
          allWarehouseItemsData.warehouse2[shelf.id] = generateItemsForShelf('warehouse2', shelf.id);
          // 确保所有货架状态为empty
          shelf.status = 'empty';
        });
      });
    } catch (resetError) {
      console.error('重置货架状态时出错:', resetError);
      // 继续执行，尝试完成刷新操作
    }

    // 检查数据结构并提取实际的物品数据数组
    let itemsArray: any[] = [];

    if (result.data) {
      if (Array.isArray(result.data)) {
        itemsArray = result.data;
      } else if (typeof result.data === 'object') {
        // 检查常见的数据结构模式，使用类型断言避免TypeScript错误
        const dataObj = result.data as Record<string, any>;

        if (dataObj.items && Array.isArray(dataObj.items)) {
          itemsArray = dataObj.items;
        } else if (dataObj.data && Array.isArray(dataObj.data)) {
          itemsArray = dataObj.data;
        } else if (dataObj.list && Array.isArray(dataObj.list)) {
          itemsArray = dataObj.list;
        } else if (dataObj.records && Array.isArray(dataObj.records)) {
          itemsArray = dataObj.records;
        } else {
          // 最后尝试将对象本身视为单个物品
          itemsArray = [result.data];
        }
      } else if (typeof result.data === 'string') {
        // 尝试解析JSON字符串
        try {
          const parsedData = JSON.parse(result.data);
          if (Array.isArray(parsedData)) {
            itemsArray = parsedData;
          } else if (parsedData.items && Array.isArray(parsedData.items)) {
            itemsArray = parsedData.items;
          } else if (parsedData.data && Array.isArray(parsedData.data)) {
            itemsArray = parsedData.data;
          } else {
            itemsArray = [parsedData];
          }
        } catch (e) {
          console.error('解析返回的字符串数据失败:', e);
          // 即使解析失败，仍然继续，因为我们已经重置了货架状态
          itemsArray = [];
        }
      }
    }

    if (itemsArray.length === 0) {
      console.warn('未能提取到物品数据或物品数据为空，仅显示空货架');
      // 更新货架状态，确保UI正确显示
      updateShelfStatusesWithItems('warehouse1');
      updateShelfStatusesWithItems('warehouse2');
      return true; // 返回true，因为清空操作是成功的
    }

    console.log(`准备处理 ${itemsArray.length} 条物品数据`);

    // 创建库位与物品的映射
    const locationToItems: Record<string, any[]> = {};
    let processedItems = 0;

    // 遍历所有物品，提取库位信息并解析
    itemsArray.forEach((item) => {
      try {
        // 查找T736603052777414658属性
        if (!item || !item.T736603052777414658) return;

        const locationInfo = item.T736603052777414658;
        console.log(`物品库位信息: ${locationInfo}`);

        // 解析库位信息，例如："二号仓库/F1/库位 1_1"
        const match = locationInfo.match(/([一二]号仓库)\/([A-Z]\d+)\/库位\s*(\d+)_(\d+)/);

        if (!match) {
          console.log(`无法解析库位信息: ${locationInfo}`);
          return;
        }

        const [, warehouseName, shelfName, layerNum, positionNum] = match;

        // 确定仓库ID
        const warehouseId = warehouseName === "一号仓库" ? "warehouse1" : "warehouse2";

        // 构建库位编号
        let locationCode;
        if (warehouseId === "warehouse1") {
          // 公物仓一的库位编号格式：AQ_HJ_001_1_1
          const area = shelfName.charAt(0); // 例如：F1 -> F
          const shelfNum = shelfName.substring(1).padStart(3, '0'); // 例如：F1 -> 001
          locationCode = `${area}Q_HJ_${shelfNum}_${layerNum}_${positionNum}`;
        } else {
          // 公物仓二的库位编号格式：FQ_HJ_001_1_1
          const area = shelfName.charAt(0); // 例如：F1 -> F
          const shelfNum = shelfName.substring(1).padStart(3, '0'); // 例如：F1 -> 001
          locationCode = `${area}Q_HJ_${shelfNum}_${layerNum}_${positionNum}`;
        }

        // 将物品添加到对应库位的列表中
        if (!locationToItems[locationCode]) {
          locationToItems[locationCode] = [];
        }
        locationToItems[locationCode].push(item);
        processedItems++;

        // 调试输出 - 输出找到的匹配
        console.log(`匹配成功: 库位信息=${locationInfo} 对应库位编号=${locationCode}`);
      } catch (itemError) {
        console.error('处理物品时出错:', itemError);
        // 继续处理下一个物品
      }
    });

    console.log(`成功处理 ${processedItems} 条物品数据到 ${Object.keys(locationToItems).length} 个库位`);

    // 遍历所有匹配到的库位，更新物品数据和货架状态
    Object.entries(locationToItems).forEach(([locationCode, locationItems]) => {
      try {
        const locationInfo = parseLocationCode(locationCode);
        if (!locationInfo) return;

        const { warehouseId, shelfId, layerId, positionId } = locationInfo;

        // 确保数据结构存在
        if (!allWarehouseItemsData[warehouseId][shelfId]) {
          allWarehouseItemsData[warehouseId][shelfId] = {};
        }
        if (!allWarehouseItemsData[warehouseId][shelfId][layerId]) {
          allWarehouseItemsData[warehouseId][shelfId][layerId] = {};
        }

        // 获取正确的库位ID
        const realId = locationIdMapping[locationCode] || '';

        // 创建物品数据 - 使用安全创建函数
        const warehouseItems: WarehouseItem[] = locationItems
          .map((item: Record<string, any>, index) =>
            createSafeWarehouseItem(item, shelfId, layerId, positionId, index, locationCode, realId)
          )
          .filter(isValidWarehouseItem); // 过滤掉无效的物品

        // 更新库位的物品数据
        allWarehouseItemsData[warehouseId][shelfId][layerId][positionId] = warehouseItems;
      } catch (locationError) {
        console.error('处理库位时出错:', locationError);
        // 继续处理下一个库位
      }
    });

    // 使用try-catch包装更新货架状态的操作
    try {
      // 更新货架的状态
      updateShelfStatusesWithItems('warehouse1');
      updateShelfStatusesWithItems('warehouse2');

      // 简化状态检查日志
      let shelvesWithItems = 0;
      Object.entries(mockWarehouseData1.shelves).forEach(([area, shelves]) => {
        shelves.forEach(shelf => {
          if (shelf.status !== 'empty') {
            shelvesWithItems++;
          }
        });
      });
      Object.entries(mockWarehouseData2.shelves).forEach(([area, shelves]) => {
        shelves.forEach(shelf => {
          if (shelf.status !== 'empty') {
            shelvesWithItems++;
          }
        });
      });
      console.log(`更新完成: ${shelvesWithItems}个货架有物品`);
    } catch (statusError) {
      console.error('更新货架状态时出错:', statusError);
      // 如果更新货架状态失败，确保所有货架至少显示为空
      updateShelfStatuses('warehouse1');
      updateShelfStatuses('warehouse2');
    }

    return true;
  } catch (error) {
    console.error('刷新物品数据失败:', error);
    // 即使发生错误，也确保UI不会崩溃
    try {
      updateShelfStatuses('warehouse1');
      updateShelfStatuses('warehouse2');
    } catch (e) {
      console.error('紧急恢复失败:', e);
    }
    return false;
  }
};

// 更新货架状态，根据有物品的层数
const updateShelfStatusesWithItems = (warehouseId: string) => {
  try {
    const warehouseData = allWarehouses[warehouseId];
    if (!warehouseData) {
      console.error(`找不到仓库: ${warehouseId}`);
      return;
    }

    Object.entries(warehouseData.shelves).forEach(([area, shelves]) => {
      try {
        // 检查shelves是否为数组
        if (!Array.isArray(shelves)) {
          console.error(`仓库 ${warehouseId} 的区域 ${area} 的货架不是数组`);
          return;
        }

        shelves.forEach((shelf: Shelf) => {
          try {
            // 检查shelf是否有效
            if (!shelf || !shelf.id) {
              console.error('发现无效的货架');
              return;
            }

            const shelfId = shelf.id;
            const shelfItemsData = allWarehouseItemsData[warehouseId]?.[shelfId] || {};

            // 检查各层是否有物品
            const layersWithItems = [];

            for (let layerNum = 1; layerNum <= 4; layerNum++) {
              try {
                const layerId = `${shelfId}-L${layerNum}`;
                if (checkLayerHasItems(shelfItemsData, layerId)) {
                  layersWithItems.push(layerNum);
                }
              } catch (layerError) {
                console.error(`检查层 ${shelfId}-L${layerNum} 时出错:`, layerError);
              }
            }

            // 根据有物品的层数设置货架状态
            const layerCount = layersWithItems.length;

            // 确保shelf.status有一个有效值
            if (layerCount === 0) {
              shelf.status = 'empty'; // 没有层有物品 - 灰色
            } else if (layerCount === 1) {
              shelf.status = 'first'; // 1层有物品 - 蓝色
            } else if (layerCount === 2) {
              shelf.status = 'second'; // 2层有物品 - 绿色
            } else if (layerCount === 3) {
              shelf.status = 'third'; // 3层有物品 - 黄色
            } else if (layerCount === 4) {
              shelf.status = 'fourth'; // 4层都有物品 - 红色
            } else {
              // 这种情况不应该发生，但为了安全，设置一个默认值
              shelf.status = 'empty';
            }
          } catch (shelfError) {
            console.error(`更新货架 ${shelf?.id || '未知'} 状态时出错:`, shelfError);
            // 确保货架状态有一个默认值
            if (shelf) shelf.status = 'empty';
          }
        });
      } catch (areaError) {
        console.error(`处理区域 ${area} 时出错:`, areaError);
      }
    });
  } catch (error) {
    console.error('更新货架状态时出错:', error);
  }
};

// 辅助函数：检查某一层是否有物品
const checkLayerHasItems = (
  shelfItemsData: Record<string, Record<string, WarehouseItem[]>>,
  layerId: string
): boolean => {
  try {
    // 检查输入参数是否有效
    if (!shelfItemsData || typeof shelfItemsData !== 'object') {
      return false;
    }

    // 检查layerId是否存在于shelfItemsData中
    if (!shelfItemsData[layerId] || typeof shelfItemsData[layerId] !== 'object') {
      return false;
    }

    // 获取该层的所有库位
    const positions = shelfItemsData[layerId];

    // 如果没有库位，则直接返回false
    if (Object.keys(positions).length === 0) {
      return false;
    }

    // 检查该层的所有库位是否有物品
    return Object.values(positions).some(items => {
      // 检查items是否为数组且长度大于0
      return Array.isArray(items) && items.length > 0 && items.some(isValidWarehouseItem);
    });
  } catch (error) {
    console.error(`检查层 ${layerId} 是否有物品时出错:`, error);
    return false;
  }
};